package demo01;

import java.util.List;
import java.util.Random;

class MyThread3 extends Thread {
    private final List<Integer> prizes;
    private final Box box1;
    private final Box box2;
    private final Random random = new Random();
    private int maxPrize = 0;
    private int totalPrize = 0;
    private int count = 0;

    public MyThread3(List<Integer> prizes, Box box1, Box box2, String name) {
        super(name);
        this.prizes = prizes;
        this.box1 = box1;
        this.box2 = box2;
    }

    public int getMaxPrize() {
        return maxPrize;
    }

    public int getTotalPrize() {
        return totalPrize;
    }

    public int getCount() {
        return count;
    }

    @Override
    public void run() {
        while (true) {	
            Integer prize;
            synchronized (prizes) {
                if (prizes.isEmpty()) {
                    break;
                }
                int index = random.nextInt(prizes.size());
                prize = prizes.remove(index);
                count++;
            }

            if (random.nextBoolean()) {
                box1.addPrize(prize);
            } else {
                box2.addPrize(prize);
            }

            updateStatistics(prize);

            try {
                Thread.sleep(100); 
            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
        }
    }

    private synchronized void updateStatistics(int prize) {
        if (prize > maxPrize) {
            maxPrize = prize;
        }
        totalPrize += prize;
    }
}